package com.haiya.common.request;

import org.apache.commons.collections4.MapUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 分页查询参数类
 * @author qiaoguoqiang
 */
public class Query extends LinkedHashMap<String, Object> {
    private static final Integer DEFAULT_CURRENT_PAGE = 1;
    private static final Integer DEFAULT_PER_PAGE = 10;
    private static final long serialVersionUID = 1L;

    private int page;
    private int limit;
    private String tenantId;

    public Query(Map<String, Object> params) {
        this.putAll(params);

        /**
         * 租户ID处理: 有就放到tenantId中
         */
        String tenantId = MapUtils.getString(params, "tenantId");
        if (tenantId == null) {
            this.tenantId = null;
        } else {
            this.tenantId = (String) tenantId;
        }

        //分页参数
        this.page = Integer.parseInt(params.getOrDefault("page", DEFAULT_CURRENT_PAGE).toString());
        this.limit = Integer.parseInt(params.getOrDefault("limit", DEFAULT_PER_PAGE).toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
